[Tracker] Use ResizeObserver instead of polling for document height#6174
[Tracker] Use ResizeObserver instead of polling for document height#6174
Conversation
| function getCurrentScrollDepthPx() { | ||
| var body = document.body || {} | ||
| var el = document.documentElement || {} | ||
| var viewportHeight = window.innerHeight || el.clientHeight || 0 |
There was a problem hiding this comment.
By removing IE support for engagement tracking, these fallbacks are not necessary since window.scrollY and window.innerHeight are well-supported:
https://caniuse.com/mdn-api_window_innerheight
https://caniuse.com/mdn-api_window_scrolly
There was a problem hiding this comment.
suggestion, non-blocking: Can we keep 0 fallback? It doesn't cost much weight-wise and then the math won't crash entirely if for some reason these values are not truthy. Positive numbers are truthy.
|
Analyzed 1026 tracker script variants for size changes. Main variants:
Important legacy variants:
Summary:
In total, 511 variants brotli size increased and 513 variants brotli size decreased. |
window.scrollY and window.innerHeight are compositor-cached values that never trigger layout recalculation, unlike el.scrollTop and el.clientHeight. The fallbacks only existed for IE which is already unsupported.
0e24d26 to
d883d1d
Compare
Changes
Replaces document height polling with
ResizeObserverto avoid forced reflows.Fixes #5727
ResizeObserveris a fairly modern API. Should be OK for the main script since overall it doesn't have worse support than fetch-with-keepalive. The only gap I was able to find is Edge versions less than 79 do not supportResizeObserverwhile fetch support goes back longer on Edge. So we will lose pre-2020 Edge versions but they have negligible usage worldwide. It would be good to have some kind of feature matrix for tracker script and check against caniuse data when we update it.Instead of keeping the old code as fallback for
compatcode, I've disabled engagement tracking completely incompatmode. Without fetch-with-keepalive engagement data isn't reliable anyways. But it should probably be checked with sites that actually use thecompatmode.Disabling engagements in
compatmode also allowed me to remove some fallbacks ingetCurrentScrollDepthPxfunction which are not relevant in modern browsers (window.scrollY and window.innerHeight are way better supported than fetch and ResizeObserver, the fallbacks were only relevant for IE).Verification
On a local page with plausible installed, I profiled a scripted journey where page is loaded and scrolled after waiting a half a second. The old code produced 27 document reflows, with this PR there were 3 reflows. None of the remaining 3 were triggered by our script, they were all internal chrome events.